# -*- coding: utf-8 -*-
import json
import re

from django.http import JsonResponse, QueryDict
# Create your views here.
from django.views.decorators.csrf import csrf_exempt

from PersonManage.models import *
from PersonManage.serializers import *


@csrf_exempt
def getTotalEmployees(request):
    if request.method == 'GET':
        print('GET')
        print(request.GET.get('id'))
        manageId = request.GET.get('id')
        driver_list = Driver.objects.filter(ManageID=manageId)
        drivers = []
        for driver in driver_list:
            drivers.append(driver.to_dict())
        return JsonResponse({'drivers': drivers}, safe=False)


@csrf_exempt
def getWorkingEmployees(request):
    if request.method == 'GET':
        print('GET')
        manageId = request.GET.get('id')
        WorkingState = request.GET.get('WorkingState')
        if WorkingState == '在职':
            ondriver_list = Driver.objects.filter(ManageID=manageId, WorkingState='在职')
            ondrivers = []
            for ondriver in ondriver_list:
                ondrivers.append(ondriver.to_dict())
            return JsonResponse({"ondrivers": ondrivers}, safe=False)
        else:
            stopdriver_list = Driver.objects.filter(ManageID=manageId, WorkingState='停职')
            stopdrivers = []
            for stopdriver in stopdriver_list:
                stopdrivers.append(stopdriver.to_dict())
            return JsonResponse({"stopdrivers": stopdrivers}, safe=False)


@csrf_exempt
def changeDriverState(request):
    if request.method == 'POST':
        print('POST')
        id = request.POST.get('id')
        WorkingState = request.POST.get('WorkingState')
        if WorkingState == '在职':
            driver = Driver.objects.get(id=id)
            driver.WorkingState = '在职'
            driver.save()
            return JsonResponse({"Message": "on working success"}, safe=False)
        elif WorkingState == '停职':
            driver = Driver.objects.get(id=id)
            StopServeDate = request.POST.get('StopServeDate')
            StopTime = request.POST.get('StopTime')
            driver.StopServeDate = StopServeDate
            driver.StopTime = StopTime
            driver.WorkingState = '停职'
            driver.save()
            return JsonResponse({"Message": "stops working success"}, safe=False)


@csrf_exempt
def ChangeComplainedTimes(request):
    if request.method == 'POST':
        id = request.POST.get('id')
        driver = Driver.objects.get(id=id)
        intTimes = int(driver.ComplainedTimes)
        strTimes = str(intTimes + 1)
        driver.ComplainedTimes = strTimes
        driver.save()
        return JsonResponse({"Message": "Change ComplainedTimes Success"}, safe=False)


@csrf_exempt
def AddHRMessage(request):
    if request.method == 'POST':
        # manageId = request.session['id']
        print(request.POST.get)
        id = request.POST.get('DrID')
        driver = Driver.objects.filter(id=id)
        if driver.exists():
            message = MessageSerializer(data=request.POST)
            if message.is_valid():
                message.save()
            else:
                return JsonResponse({"Message": "Arguments Illegal"}, safe=False)
            return JsonResponse({"Message": "Add message Success"}, safe=False)
        else:
            return JsonResponse({"Message": "Driver Does Not Exist"}, safe=False)


@csrf_exempt
def AddNewDriver(request):
    if request.method == 'POST':
        print(request.POST.get)
        id = request.POST.get('id')
        an = re.search('^Dr[0-9]{3}$', id)
        if an:
            driver = Driver.objects.filter(id=id)
            if driver.exists():
                return JsonResponse({"Message": "Driver exists"}, safe=False)
            else:
                newDriver = DriverSerializer(data=request.POST)
                #print(newDriver)
                if newDriver.is_valid():
                    newDriver.save()
                else:
                    print(newDriver.errors)
                    return JsonResponse({"Message": "Arguments Illegal"}, safe=False)
                return JsonResponse({"Message": "Add new driver Success"}, safe=False)
        else:
            return JsonResponse({"Message:": "id is illegal"}, safe=False)


@csrf_exempt
def addFeedBack(request):
    if request.method == 'POST':
        print(request)
        CusServeID = request.POST.get('CusServeID')
        CustomerID = request.POST.get('CustomerID')
        Details = request.POST.get('Details')
        DriverID = request.POST.get('DriverID')
        messageType = request.POST.get('messageType')
        feedback = FeedBack()
        driver = Driver.objects.get(id=DriverID)
        feedback.CusServeID = CusServeID
        feedback.CustomerID = CustomerID
        feedback.Details = Details
        feedback.DriverID = DriverID
        feedback.messageType = messageType
        feedback.ManagerID = driver.ManageID
        feedback.save()
        return JsonResponse({"Message:": "add Feedback success"}, safe=False)


@csrf_exempt
def getFeedBack(request):
    if request.method == 'GET':
        print('GET')
        ManagerID = request.GET.get('id')
        feedback_list = FeedBack.objects.filter(ManagerID=ManagerID)
        feedbacks = []
        for feedback in feedback_list:
            feedbacks.append(feedback.to_dict2())
        return JsonResponse({"feedbacks": feedbacks}, safe=False)


@csrf_exempt
def changeFeedBackState(request):
    if request.method == 'POST':
        print('POST')
        id = request.POST.get('id')
        WorkingState = request.POST.get('WorkingState')
        if WorkingState == '不显示':
            driver = Driver.objects.get(id=id)
            driver.WorkingState = '在职'
            driver.save()
            return JsonResponse({"Message": "on working success"}, safe=False)
        elif WorkingState == '停职':
            driver = Driver.objects.get(id=id)
            StopServeDate = request.POST.get('StopServeDate')
            StopTime = request.POST.get('StopTime')
            driver.StopServeDate = StopServeDate
            driver.StopTime = StopTime
            driver.WorkingState = '停职'
            driver.save()
            return JsonResponse({"Message": "stops working success"}, safe=False)


@csrf_exempt
def changeFeedBackState(request):
    if request.method == 'POST':
        print('POST')
        CusServeID = request.POST.get('CusServeID')
        DriverID = request.POST.get('DriverID')
        messageType = request.POST.get('messageType')
        CustomerID = request.POST.get('CustomerID')
        Details = request.POST.get('Details')
        FeedBackState = request.POST.get('FeedBackState')
        feedback = FeedBack.objects.filter(CusServeID=CusServeID, DriverID=DriverID, messageType=messageType,
                                           CustomerID=CustomerID, Details=Details)
        if feedback.exists:
            if FeedBackState == '不显示':
                feedback.delete()
                return JsonResponse({"Message": "change/delete feedback success"}, safe=False)
        else:
            return JsonResponse({"Message": "feedback not exists"}, safe=False)


@csrf_exempt
def deleteDriver(request):
    if request.method == 'POST':
        id = request.POST.get('id')
        try:
            driver = Driver.objects.get(id=id)
            driver.delete()
            return JsonResponse({"Message": "delete driver success"}, safe=False)
        except Driver.DoesNotExist:
            return JsonResponse({"Message": "driver not exist"}, safe=False)


@csrf_exempt
def getComplainTimes(request):
    if request.method == 'GET':
        print(request.GET.get('id'))
        id = request.GET.get('id')
        try:
            driver = Driver.objects.get(id=id)
            return JsonResponse({"ComplainTimes": driver.ComplainedTimes}, safe=False)
        except Driver.DoesNotExist:
            return JsonResponse({"Message": "driver not exist"}, safe=False)


@csrf_exempt
def checkDriverInfo(request):
    if request.method == 'GET':
        print('GET')
        id = request.GET.get('id')
        driver = Driver.objects.get(id=id)
        return JsonResponse({"Info": driver.to_dict9()}, safe=False)